package cn.wolfcode.crm.Service.impl;


import cn.wolfcode.crm.Service.IPermissionService;
import cn.wolfcode.crm.annotation.RequiredPermission;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.PermissionQuery;
import cn.wolfcode.crm.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        int i = permissionMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Autowired
    private ApplicationContext application;

    @Override
    public int insert(Permission record) {
        int i = permissionMapper.insert(record);
        return i;
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> selectAll() {
        List<Permission> permissionList = permissionMapper.selectAll();
        return permissionList;
    }

    @Override
    public int updateByPrimaryKey(Permission record) {
        int i = permissionMapper.updateByPrimaryKey(record);
        return i;
    }



    @Override
    public PageInfo<Permission> queryForList(PermissionQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> list = permissionMapper.queryForList(qo);
        return new PageInfo<>(list);

    }

    @Override
    public void reload() {
        //再次加载时不应该出现重复的参数
        //先查询出数据库保存的数据
        //获取到所有控制器的对象
        Map<String, Object> beans = application.getBeansWithAnnotation(Controller.class);
        //得到控制器对象
        Collection<Object> values = beans.values();
        //遍历controller对象里的方法
        for (Object controller : values) {
            //得到控制器里的所有的方法
            //代理类需要获取父类中的方法
            Method[] methods = controller.getClass().getSuperclass().getDeclaredMethods();
            //遍历控制器里的方法,判断是否有requestPermission注解
            for (Method method : methods) {
                boolean isExists = method.isAnnotationPresent(RequiredPermission.class);
                if (isExists) {
                    /*//得到当前方法的简单名字
                    //String methodName = method.getName();
                    String expression = StringUtils.getExpression(method);*/
                    //判断当前表达式的名字是否存在,存在则不保存
                    List<Permission> permissions = permissionMapper.queryForList(new PermissionQuery());
                    HashSet<String> permissionExperssions = new HashSet<>();
                    for (Permission permission : permissions) {
                        permissionExperssions.add(permission.getExpression());
                    }
                    //获取权限表达式
                    RequiresPermissions requiredPermissionAnno = method.getAnnotation(RequiresPermissions.class);
                    String expression = requiredPermissionAnno.value()[1];
                    if (!permissionExperssions.contains(expression)) {
                        Permission permission = new Permission();
                        permission.setName(requiredPermissionAnno.value()[0]);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }
}

